package com.example.study.test.suanfa;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: https://mimi-21.com/
 * @date: 2020/11/23 11:01 下午
 * @description:
 */
public class ShuZuCiShu {
    public static void main(String[] args) {
        int[] array = {1,2,1,2,3,4,5,5};
        List<Integer> integers = sumTimes(array);
        System.out.println(integers.toString());
    }

    /**
     * @description: 求出数组中出现次数大于一的元素
     * @author: nannan
     * @date: 2020/11/23 11:11 下午
     * @param array:
     * @return: java.util.List<java.lang.Integer>
     */
    public static List<Integer> sumTimes(int[] array) {
        Map<Integer, Integer> resultMap = new HashMap<>(array.length);
        for (int i : array) {
            if (resultMap.containsKey(i)) {
                Integer value = resultMap.get(i);
                resultMap.put(i, ++value);
            } else {
                resultMap.put(i, 1);
            }
        }
        List<Integer> resultList = resultMap.entrySet().stream().filter(model -> model.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
        return resultList;
    }
}
